package com.hncc.dms.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.hncc.dms.domain.DmsCaseModel;
import com.hncc.dms.mapper.DmsCaseModelMapper;
import com.hncc.sms.mapper.SysUserManagerMapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.hncc.dms.mapper.DmsCaseModelCatalogMapper;
import com.ruoyi.common.core.domain.entity.DmsCaseModelCatalog;
import com.hncc.dms.service.IDmsCaseModelCatalogService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 病例模版管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-22
 */
@Service
public class DmsCaseModelCatalogServiceImpl implements IDmsCaseModelCatalogService 
{
    @Resource
    private DmsCaseModelCatalogMapper dmsCaseModelCatalogMapper;

    @Resource
    private SysUserManagerMapper sysUserManagerMapper;

    @Resource
    private DmsCaseModelMapper dmsCaseModelMapper;

    /**
     * 查询病例模版管理
     * 
     * @param id 病例模版管理ID
     * @return 病例模版管理
     */
    @Override
    public DmsCaseModelCatalog selectDmsCaseModelCatalogById(Long id)
    {
        return dmsCaseModelCatalogMapper.selectDmsCaseModelCatalogById(id);
    }

    /**
     * 查询病例模版管理列表
     * 
     * @param dmsCaseModelCatalog 病例模版管理
     * @return 病例模版管理
     */
    @Override
    public List<DmsCaseModelCatalog> selectDmsCaseModelCatalogList(DmsCaseModelCatalog dmsCaseModelCatalog)
    {
        return dmsCaseModelCatalogMapper.selectDmsCaseModelCatalogList(dmsCaseModelCatalog);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param dmsCaseModelCatalogs 病历模板目录列表
     * @return 树结构列表
     */
    @Override
    public List<DmsCaseModelCatalog> buildDeptTree(List<DmsCaseModelCatalog> dmsCaseModelCatalogs) {
        List<DmsCaseModelCatalog> returnList = new ArrayList<DmsCaseModelCatalog>();
        List<Long> tempList = new ArrayList<Long>();
        for (DmsCaseModelCatalog dmsCaseModelCatalog : dmsCaseModelCatalogs)
        {
            tempList.add(dmsCaseModelCatalog.getId());
        }
        for (Iterator<DmsCaseModelCatalog> iterator = dmsCaseModelCatalogs.iterator(); iterator.hasNext();)
        {
            DmsCaseModelCatalog catalog = (DmsCaseModelCatalog) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(catalog.getParentId()))
            {
                recursionFn(dmsCaseModelCatalogs, catalog);
                returnList.add(catalog);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = dmsCaseModelCatalogs;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param dmsCaseModelCatalog 病历模板目录列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<DmsCaseModelCatalog> dmsCaseModelCatalog) {
        List<DmsCaseModelCatalog> deptTrees = buildDeptTree(dmsCaseModelCatalog);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 新增病例模版管理
     * 
     * @param dmsCaseModelCatalog 病例模版管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDmsCaseModelCatalog(DmsCaseModelCatalog dmsCaseModelCatalog)
    {
        //根据登录名查询登录id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //所属人Id
        dmsCaseModelCatalog.setOwnId(userId);
        //创建时间
        dmsCaseModelCatalog.setCreateTime(DateUtils.getNowDate());

        //新增模板基础信息
        DmsCaseModel dmsCaseModel = new DmsCaseModel();
        dmsCaseModel.setName(dmsCaseModelCatalog.getName());
        //拿到模板id
        dmsCaseModelMapper.insertDmsCaseModel(dmsCaseModel);
        System.out.println("新增模板信息id--->>>"+dmsCaseModel.getId());
        dmsCaseModelCatalog.setModelId(dmsCaseModel.getId());
        dmsCaseModelCatalogMapper.insertDmsCaseModelCatalog(dmsCaseModelCatalog);
        return dmsCaseModel.getId().intValue();
    }

    /**
     * 修改病例模版管理
     * 
     * @param dmsCaseModelCatalog 病例模版管理
     * @return 结果
     */
    @Override
    public int updateDmsCaseModelCatalog(DmsCaseModelCatalog dmsCaseModelCatalog)
    {
        return dmsCaseModelCatalogMapper.updateDmsCaseModelCatalog(dmsCaseModelCatalog);
    }

    /**
     * 批量删除病例模版管理
     * 
     * @param ids 需要删除的病例模版管理ID
     * @return 结果
     */
    @Override
    public int deleteDmsCaseModelCatalogByIds(Long[] ids)
    {
        return dmsCaseModelCatalogMapper.deleteDmsCaseModelCatalogByIds(ids);
    }

    /**
     * 删除病例模版管理信息
     * 
     * @param id 病例模版管理ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDmsCaseModelCatalogById(Long id)
    {
        //查询模板id
        DmsCaseModelCatalog dmsCaseModelCatalog = dmsCaseModelCatalogMapper.selectDmsCaseModelCatalogById(id);
        //判断是模板还是目录
        if(dmsCaseModelCatalog.getModelId() != null){
            //根据模板id删除模板
            dmsCaseModelMapper.deleteDmsCaseModelById(dmsCaseModelCatalog.getModelId());
        }
        //删除目录/模板
        return dmsCaseModelCatalogMapper.deleteDmsCaseModelCatalogById(id);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<DmsCaseModelCatalog> list, DmsCaseModelCatalog t)
    {
        // 得到子节点列表
        List<DmsCaseModelCatalog> childList = getChildList(list, t);
        t.setChildren(childList);
        for (DmsCaseModelCatalog tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<DmsCaseModelCatalog> it = childList.iterator();
                while (it.hasNext())
                {
                    DmsCaseModelCatalog n = (DmsCaseModelCatalog) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }



    /**
     * 得到子节点列表
     */
    private List<DmsCaseModelCatalog> getChildList(List<DmsCaseModelCatalog> list, DmsCaseModelCatalog t)
    {
        List<DmsCaseModelCatalog> tlist = new ArrayList<DmsCaseModelCatalog>();
        Iterator<DmsCaseModelCatalog> it = list.iterator();
        while (it.hasNext())
        {
            DmsCaseModelCatalog n = (DmsCaseModelCatalog) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<DmsCaseModelCatalog> list, DmsCaseModelCatalog t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
